#include "ET++.ph"
#ifdef __GNUG__
#pragma implementation
#endif

#include "DirectoryDialog.h"

#include "Class.h"
#include "Buttons.h"
#include "FileData.h"
#include "Fields.h"
#include "System.h"
#include "Box.h"

const int cIdDirOpen  =   cIdFirstUser + 130;

//---- DirectoryDialog ---------------------------------------------------------

NewMetaImpl0(DirectoryDialog,FileDialog);

DirectoryDialog *DirectoryDialog::gDirectoryDialog;

const char *DirectoryDialog::GetDirectory(Window *w, EvtHandler *eh)
{
	if (gDirectoryDialog == 0)
		gDirectoryDialog= new DirectoryDialog;

	if (gDirectoryDialog->ShowInWindow(eFDRead, w, eh, "Directory Name") == cIdOk)
		return gSystem->WorkingDirectory();
	return 0;
}

ONEXIT(DirectoryDialog)
{
	SafeDelete(DirectoryDialog::gDirectoryDialog);
}

DirectoryDialog::DirectoryDialog() : FileDialog("Directory Dialog")
{
}

Data *DirectoryDialog::FilterData(Data *data)
{
	return data->IsDirectory() ? data : 0;
}

VObject *DirectoryDialog::DoMakeActions()
{
	return
		new HBox(20, (VObjAlign)(eVObjVBase|eVObjHEqual|eVObjHExpand),
			openButton= new ActionButton(cIdOk, "Open", TRUE),
			selectButton= new ActionButton(cIdDirOpen, "Select"),
			new ActionButton(cIdCancel, "Cancel"),
			new ActionButton(cIdUpdate, "Update"),
			0
		);
}

void DirectoryDialog::DoSetup()
{
	bool e= FALSE;
	if (eti->GetTextSize() > 0) {
		char pathname[cPathBuf];
		eti->GetString(pathname, cPathBuf);

		if (gSystem->ExpandPathName(pathname, cPathBuf-1))
			return;

		FileData *fd= new FileData(pathname);
		e= fd->IsDirectory();
		delete fd;
	}
	openButton->Enable(e);
	selectButton->Enable(e);
}

void DirectoryDialog::Control(int id, int p, void *v)
{
	if (id == cIdDirOpen) {
		OpenOrChangeDir();
		Dialog::Control(cIdOk, p, v);
	} else
		FileDialog::Control(id, p, v);
}

